/* ========================================================================
 * Kangaroo: inform.js
 *
 * ======================================================================== */

+function($) {
    "use strict";

    var Inform = function(options) {
        this.options    = options;
        this.$body      = $(document.body);
        this.$informEle = null;
        this.delayTimer = null;
    };

    Inform.VERSION = "0.0.1";

    Inform.DEFAULTS = {
        type: "general",  // type can be general, success, error and warn
        title: "",
        delay: 2000,
        size: 'normal',
        cb: null,
        close: false
    };

    Inform.prototype.init = function() {
        var informEle = "";
        if (this.options.close) {
            informEle = "<div class=\"inform fade inform-"+ this.options.type +" "+ this.options.size +"\" data-type=\"kg.inform\"><div class=\"close\" data-dismiss=\"inform\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></div><p><i></i><span>"+ this.options.title +"</span></p></div>";
        } else {
            informEle = "<div class=\"inform fade inform-"+ this.options.type +" "+ this.options.size +"\" data-type=\"kg.inform\"><p><i></i><span>"+ this.options.title +"</span></p></div>";
        }
        this.$informEle = $(informEle);
        this.$informCont = this.$informEle.find('p');
        this.$body.append(this.$informEle);
        window.kgInform = this.$informEle;
        this.reposition();
        this.show();
        return this;
    };

    Inform.prototype.destroy = function() {
        !!this.delayTimer && clearTimeout(this.delayTimer);
        this.$informEle.remove();
        (this.options.cb instanceof Function) && this.options.cb();
    };

    Inform.prototype.show = function() {
        this.$informEle.addClass('in');
        if (!this.options.close) {
            var that = this;
            this.delayTimer = setTimeout(function() {
                that.$informEle.removeClass('in');
                setTimeout(function() {
                    that.destroy();
                }, 200);
            }, that.options.delay);
        } else {
            this.$informEle.on('click.dismiss.kg.inform', '[data-dismiss="inform"]', $.proxy(this.destroy, this));
        }
    };

    Inform.prototype.reposition = function() {
        this.$informEle.css({
            "margin-left": -Math.max(0, this.$informEle.width() / 2),
            "margin-top": -Math.max(0, this.$informEle.height() / 2)
        });
        if (this.options.size === 'large') {
            this.$informCont.css({
                "margin-top": -Math.max(0, this.$informCont.height() / 2)
            });
        }
    };

    // INFORM PLUGIN DEFINITION
    // =======================

    function Plugin(option) {
        var $oldInform = $('body > .inform');
        if ($oldInform.data('type') === 'kg.inform') {
            $oldInform.remove();
        }
        var options = $.extend({}, Inform.DEFAULTS, option);
        return new Inform(options).init();
    }

    var old = $.fn.inform;

    $.fn.inform             = Plugin;
    $.fn.inform.Constructor = Inform;


    // INFORM NO CONFLICT
    // =================

    $.fn.inform.noConflict = function () {
        $.fn.inform = old;
        return this;
    };


    // INFORM DATA-API
    // ==============

    $(document).on('click.kg.inform.data-api', '[data-toggle="inform"]', function (e) {
        var $this   = $(this);
        var option  = $this.data();
        if ($this.is('a')) e.preventDefault();
        Plugin(option);
    })
}(jQuery);